<?

	/*
	
		This script checks the current database layout and updates it if necessary. 
		This script is updated once per change and is able to upgrade an install across serveral version changes.
	
	*/
	$server_path = './';
	
	require( $server_path."ccds/base/ccdsConfigParser.inc.php" );
	require( $server_path."ccds/base/ccdsConfig.inc.php" );
	require( $server_path."ccds/base/ccdSQL.inc.php" );


	$config = ccdsConfig::getInstance();
	$ccdSQL = ccdSQL::getInstance();
	$mysql_conf = $config->get('mysql');

	$db_name = $mysql_conf['servers']['slave']['db'];

	$query = "SHOW TABLES FROM ".$db_name;
	$res = $ccdSQL->query($query);

	$tables = array();	
	while ($row = $ccdSQL->fetch_array($res)) {
		$tables[] = $row[0];
	}
	
	

	if (!in_array('user_log',$tables)) 
	{
		print "table user_log does not exist<br />";


		$query = "
			CREATE TABLE IF NOT EXISTS `user_log` (
		  `userlog_id` int(11) NOT NULL auto_increment,
		  `userlog_key_user_id` int(10) unsigned NOT NULL,
		  `userlog_key_obj_id` int(10) unsigned NOT NULL,
		  `userlog_action` varchar(200) NOT NULL,
		  `userlog_timestamp` bigint(20) unsigned NOT NULL,
		  PRIMARY KEY  (`userlog_id`)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1
		";
		$res = $ccdSQL->query($query);
	}
	
	if (!in_array('obj_versions_index',$tables)) 
	{
		print "table obj_versions_index does not exist<br />";
		
		$query = "RENAME TABLE `".$db_name."`.`obj_versions`  TO `".$db_name."`.`obj_versions_index`" ;
		$res = $ccdSQL->query($query);
		$query = "
			CREATE TABLE IF NOT EXISTS `obj_versions` (
			  `obj_vers_id` bigint(20) unsigned NOT NULL,
			  `obj_vers_key_obj_id` bigint(20) unsigned NOT NULL default '0',
			  `obj_vers_key_lang_id` int(11) NOT NULL default '0',
			  PRIMARY KEY  (`obj_vers_id`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8;
		";
		$res = $ccdSQL->query($query);
		
		$query = "SELECT obj_vers_id,obj_vers_key_obj_id,obj_vers_key_lang_id FROM obj_versions_index WHERE obj_vers_visible=1";
		$res = $ccdSQL->query($query);
		while ($row = $ccdSQL->fetch_assoc($res)) 
		{
			$ccdSQL->query("INSERT INTO obj_versions VALUES('".$row['obj_vers_id']."','".$row['obj_vers_key_obj_id']."','".$row['obj_vers_key_lang_id']."')");
		}
	}
	
	$query = " SELECT * FROM obj_index LIMIT 1 ";
	$res = $ccdSQL->query($query);
	$row = $ccdSQL->fetch_assoc($res);
	
	if (!isset($row['obj_index_no_elements'])) 
	{
		$query = "ALTER TABLE `obj_index` ADD `obj_no_elements` INT( 1 ) UNSIGNED NOT NULL DEFAULT '0';";
		$ccdSQL->query($query);
		print "added obj_no_elements TO obj_index<br />";
	}


	if (!isset($row['obj_preview'])) 
	{
		$query = "ALTER TABLE `obj_index` ADD COLUMN `obj_preview` tinyint(1) NOT NULL DEFAULT 1 AFTER `obj_index_no_elements`";
		$ccdSQL->query($query);
		print "added obj_preview TO obj_index<br />";
	}

	

	if (!in_array('modules',$tables)) 
	{
		print "table modules does not exist<br />";	
		$query = "CREATE TABLE IF NOT EXISTS `modules` (
		  `module_id` int(10) unsigned NOT NULL auto_increment,
		  `module_name` varchar(80) NOT NULL,
		  `module_key_project_id` int(10) unsigned NOT NULL,
		  `module_active` int(1) NOT NULL default '0',
		  PRIMARY KEY  (`module_id`)
		) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0 ;
		";
		$ccdSQL->query($query);
			
	}
	

	if (!in_array('modules_configuration',$tables)) 
	{
		print "table modules_configuration does not exist<br />";	
		$query = "
			CREATE TABLE IF NOT EXISTS `modules_configuration` (
			  `module_config_name` varchar(80) character set utf8 NOT NULL,
			  `module_config_key` varchar(20) character set utf8 NOT NULL,
			  `module_config_value` text character set utf8 NOT NULL,
			  `module_config_key_project_id` int(10) unsigned NOT NULL,
			  PRIMARY KEY  (`module_config_name`,`module_config_key`,`module_config_key_project_id`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8;
		";
		$ccdSQL->query($query);
			
	}


	$query = " SELECT * FROM user LIMIT 1 ";
	$res = $ccdSQL->query($query);
	$row = $ccdSQL->fetch_assoc($res);
	
	if (!isset($row['user_dir'])) {
		$query = "ALTER TABLE `user` ADD `user_dir` VARCHAR( 200 ) NOT NULL";
		$ccdSQL->query($query);
		print "added user_dir TO user<br />";
	}	
		